import 'package:demo/app/routes/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../../../model/agreement_model.dart';
import '../../../../model/bank_model.dart';
import '../../../../model/form_item_model.dart';
import '../../../../model/repay_plan_model.dart';
import '../../../../theme/app_colors.dart';
import '../../../../theme/app_text_theme.dart';
import '../../../widgets/application_scroll_table_page.dart';

class LoanController extends GetxController {

  var loanNum = ''; // 订单编号
  var isFullApi = false.obs;// 是否是全流程产品，半流程不显示银行卡
  var isSelectAgreement = false.obs;// 是否选择协议

  var loanMoney = 0.0.obs;// 贷款金额 单位改为元
  var canLoanMin = 0.0.obs;// 最少贷款金额 单位改为元
  var canLoanMax = 0.0.obs;// 最多贷款金额 单位改为元
  var selectBank = Bank().obs;// 选择的银行卡

  var termList = [].obs; // 期限
  var rate = 0.0.obs;// 利率
  var interest = 0.0.obs;// 利息
  var repayPlanList = [].obs; // 还款计划
  var chooseTerm = ''.obs; // 选择的期限
  var agreementList = [].obs;// 协议

  @override
  void onInit() {
    super.onInit();
    loanNum = Get.arguments['loan_num'];
    getOrderInfo();
  }

  /// 获取订单详情
  void getOrderInfo() {
    isFullApi.value = true;
    // 拉取协议，我已阅读并同意《个人单笔消费借款合同》
    // var temp = [{'name':'协议一','url':''},{'name':'协议二','url':''},{'name':'协议三','url':''}];
    // agreementList.clear();
    // agreementList.addAll(temp);


    // 拉取审核结果，获取额度啥的
    var min = 1004.0;
    var max = 20001.0;
    canLoanMin.value = (min / 1000).ceil() * 1000.0;// 最少贷款金额 单位改为元
    canLoanMax.value = (max / 1000).floor() * 1000.0;// 最多贷款金额 单位改为元
    loanMoney.value = canLoanMax.value;// 贷款金额 单位改为元
    termList.clear();
    var tempList = <IdAndName>[];
    tempList.add(IdAndName(id:'1',name: '12期',select: false));
    tempList.add(IdAndName(id:'2',name: '9期',select: false));
    tempList.add(IdAndName(id:'3',name: '6期',select: false));
    tempList.add(IdAndName(id:'4',name: '3期',select: false));
    termList.addAll(tempList);
    rate.value = 1.7;
    interest.value = 999;
    // 去试算
    trial();
  }

  /// 试算,拿还款计划
  void trial(){
    agreementList.clear();
    var tempList = <Agreement>[];
    tempList.add(Agreement(url:'https://www.baidu.com',name: '协议1'));
    tempList.add(Agreement(url:'https://www.baidu.com',name: '协议2'));
    agreementList.addAll(tempList);

    repayPlanList.clear();
    var tempList1 = <IdAndName>[];
    tempList1.add(IdAndName(id:'1',name: '12期',select: false));
    tempList1.add(IdAndName(id:'4',name: '3期',select: false));
    repayPlanList.addAll(tempList);
  }

  /// 选择银行卡
  void chooseBank(){
    /// 获取绑定的银行卡
    /// 如果没有，直接去绑定页面 JumpCenter.toAddBank(orderId)
    /// 有的话底部弹窗，有选择，有一个新加按钮
    var bankList = [
      Bank(id: 1,no: '6215590200009294083',bank: 'ICBC',phone: '15202653100',name: '工商银行'),
      Bank(id: 2,no: '6215590200009294152',bank: 'ICBC',phone: '15202653100',name: '中国银行'),
      Bank(id: 3,no: '6215590200009294196',bank: 'ICBC',phone: '15202653100',name: '北京银行'),
      Bank(id: 4,no: '6215590200009294302',bank: 'ICBC',phone: '15202653100',name: '上海银行'),
      Bank(id: 5,no: '6215590200009294123',bank: 'ICBC',phone: '15202653100',name: '华夏银行'),
      Bank(id: 0,no: '6215590200009294123',bank: 'ICBC',phone: '15202653100',name: '添加新的银行卡'),
    ];

    Get.bottomSheet(
      Container(
        padding: EdgeInsets.symmetric(horizontal: 15.w),
        decoration: BoxDecoration(
          color: Color_white,
          borderRadius: BorderRadius.vertical(top: Radius.circular(8.w)),
        ),
        child: ListView.separated(
          shrinkWrap : true,
          padding: EdgeInsets.zero,
          itemCount: bankList.length,
          separatorBuilder: (BuildContext context, int index) => Container(color:Color_line,height: 0.2),
          itemBuilder: (BuildContext context, int index) {
            var bean = bankList[index];
            var str = '${bean.name}${bean.id!=0?' (${bean.no.substring(bean.no.length - 4,bean.no.length)})':''}';
            return InkWell(
              onTap: () => {
                Get.back(),
                if(bean.id == 0){
                  Get.toNamed(Routes.ADD_BANK,arguments: {'loan_num':loanNum})
                }else{
                  selectBank.value = bean
                },
              },
              child: SizedBox(
                height: 40.w,
                child: Align(alignment : Alignment.centerLeft, child: Text(str,style: text_131732_14)),
              ),
            );
          },
        ),
      ),
    );
  }

  /// 选择期限
  void selectTerm(int index) {
    var temp = [];
    for(IdAndName i in termList){
      i.select = i.id == termList[index].id;
      temp.add(i);
    }
    termList.clear();
    termList.addAll(temp);
    chooseTerm.value = termList[index].id;
    trial();
  }

  /// 还款计划
  void repayPlan(){
    var repayPlanList = [
      RepayPlan(qs: 1,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 2,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 3,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 4,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 5,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 6,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 7,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 8,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 9,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 10,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 11,money: '999',mark: '啊实打实大所阿打算'),
      RepayPlan(qs: 12,money: '999',mark: '啊实打实大所阿打算'),
    ];

    Get.bottomSheet(
      ApplicationScrollTablePage(list:repayPlanList),
    );
  }

  /// 协议列表
  void toAgreementList(){
    var map = {
      'title': '个人单笔消费借款合同',
      'list': agreementList.value,
    };
    Get.toNamed(Routes.AGREEMENT_LIST, arguments: map);
  }

  /// 下一步
  next() {
    EasyLoading.showToast('下一步');
    // if (vm.showBank.value!! && vm.selectBank.value == null) {
    //   "请添加银行卡".showShort()
    //   return
    // }
    // if (vm.agreementList.value != null && vm.agreementList.value!!.size > 0 && !vm.selectAgreement.value!!){
    //   "请阅读相关协议".showShort()
    //   return
    // }
    // vm.submit(orderId, object : CallbackT<Int> {
    // override fun result(data: Int) {
    // "下单成功".showShort()
    // onBackPressed()
    // }
    // })
    EasyLoading.showToast('下单成功');
    Get.back();
  }


}
